<%--
  Created by IntelliJ IDEA.
  User: asus
  Date: 2019/7/10
  Time: 17:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="../Css/css.css">
    <script src="../Js/jquery.min.js"></script>
</head>
<body onLoad="createCode()" background="../Img/loginbackground.png">
<form action="#">
    <div class="div0">
        <div class="div1">文&nbsp;章&nbsp;管&nbsp;理&nbsp;系&nbsp;统</div>
        <div  class="div8" style="border-radius: 6px;">
            <div class="div6">
                <div class="biao">注册</div>
                <div class="biao1"><a href="Login.jsp">登录</a></div>
            </div>
            <div class="div6"><input type="text" class="div2" name="" placeholder="用户名/邮箱" id="username" style="border-radius: 6px;" ><span id="s" class="font" ></span></div>
            <div class="div6"><input type="password" class="div2" name="" placeholder="密码" id="password" style="border-radius: 6px;"></div>
            <div class="div6"><input type="password" class="div2" name="" placeholder="确认密码" id="repassword" style="border-radius: 6px;"></div>
            <div class="div6"><input type="text" class="div2" name="" placeholder="验证码" id="validCode" style="border-radius: 6px"></div>
            <div class="div61"><input type="text" onClick="createCode()" readonly="readonly" id="checkCode" class="unchanged" style="width: 80px;cursor:pointer"  /></div>
            <div class="div6"><input type="submit" value="注册" class="div3" id="addUser"  onclick="validate();" style="border-radius: 6px;" width="200px" ></div>
        </div>
    </div>
</form>
<script language="javascript" type="text/javascript">
    var code ; //在全局 定义验证码
    function createCode()
    {
        code = "";
        var codeLength = 4;//验证码的长度
        var checkCode = document.getElementById("checkCode");
        var selectChar = new Array(0,1,2,3,4,5,6,7,8,9);//所有候选组成验证码的字符，当然也可以用中文的
        for(var i=0;i<codeLength;i++)
        {
            var charIndex = Math.floor(Math.random()*9);
            code +=selectChar[charIndex];
        }
        if(checkCode)
        {
            checkCode.className="code";
            checkCode.value = code;
            checkCode.blur();
        }
    }
    function validate () {
        var inputCode = document.getElementById("validCode").value;
        var password = document.getElementById("password").value;
        var username = document.getElementById("username").value;
        var repassword = document.getElementById("repassword").value;
        if (username.length <= 0) {
            alert("请输入用户名/邮箱！");
            return;
        } else if (password.length <= 0) {
            alert("请输入密码！");
            return;
        } else if (repassword.length <= 0) {
            alert("请再次输入密码！");
            return;
        } else if (inputCode.length <= 0) {
            alert("请输入验证码！");
            return;
        } else if (password !== repassword) {
            alert("两次输入密码不一致！");
            return;
        } else if (inputCode.toUpperCase() != code) {
            alert("验证码输入错误！");
            createCode();//刷新验证码
        }
    }
        var addUser = function(params){
            $.ajax({
                type:"post",
                async:"true",
                url:"http://192.168.30.119:8080/user/adduser",
                dataType:"json",
                data:params,
                success:function(data){
                    debugger
                    alert("success");
                },
                error:function(data){
                    debugger
                    alert("error");
                }
            });
        }

        var getParams = function(){
            var params = {};
            params["user_name"] = $("#username").val();
            params["password"] = $("#password").val();
            return params;
        }

        $(document).ready(function(){
            $("#addUser").on("click",function(){
                addUser(getParams());
            });
        });





</script>
</body>
</html>